package com.gyt.manage.function.controller.risk;

import com.gyt.data.service.FunctionService;
import com.gyt.framework.domain.function.req.FunctionTypeReq;
import com.gyt.framework.domain.function.result.FunctionRelatedSceneResult;
import com.gyt.framework.domain.function.risk.FunctionLibraryInfo;
import com.gyt.framework.exception.BaseException;
import com.gyt.framework.response.Response;
import com.gyt.framework.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @Author created by v_yantgao
 * @Date 2021/1/7 16:50
 */
@RestController
@RequestMapping("resource/function")
@Api(tags = "resource-functionLibrary",description = "资源中心-函数库")
public class FunctionController extends BaseController {

    @Autowired
    private FunctionService functionService;

    @PostMapping("importByExcel.do")
    @ApiOperation(value = "获取Excel信息", notes = "函数库")
    public Response<?> importByExcel(@ApiParam(
            value = "文件", required = true) @RequestParam("file") MultipartFile file){
        try {
            List<String> list = functionService.importByExcel(file.getInputStream());
            return Response.ok(list);
        } catch (BaseException e) {
            logger.error("importByExcel BaseException",e);
            return Response.error(e,null);
        } catch (Exception e){
            logger.error("importByExcel BaseException",e);
            return Response.error(e,null);
        }
    }
    @GetMapping("getExcelInfo.do")
    @ApiOperation(value = "获取Excel信息", notes = "函数库")
    public Response<?> getExcelInfo(){
        try {
            List<String> list = functionService.getExcelInfo();
            return Response.ok(list);
        } catch (BaseException e) {
            logger.error("getExcelInfo BaseException",e);
            return Response.error(e,null);
        } catch (Exception e){
            logger.error("getExcelInfo BaseException",e);
            return Response.error(e,null);
        }
    }
    @GetMapping("getFunctionLibraryAllInfo.do")
    @ApiOperation(value = "获取所有函数类型信息", notes = "函数库")
    public Response<List<FunctionLibraryInfo>> getFunctionLibraryAllInfo(){
        try {
            List<FunctionLibraryInfo> libraryInfos = functionService.getFunTypeInfoList();
            return Response.ok(libraryInfos);
        } catch (BaseException e) {
            logger.error("getFunctionLibraryAllInfo BaseException",e);
            return Response.error(e,null);
        } catch (Exception e){
            logger.error("getFunctionLibraryAllInfo BaseException",e);
            return Response.error(e,null);
        }
    }
    @PostMapping("downloadPdf.do")
    @ApiOperation(value = "下载PDF", notes = "函数库")
    public Response<?> downloadPdf(){
        try {
            functionService.downloadPdf(request, response);
            return Response.ok(null);
        } catch (BaseException e) {
            logger.error("downloadPdf BaseException",e);
            return Response.error(e,null);
        } catch (Exception e){
            logger.error("downloadPdf BaseException",e);
            return Response.error(e,null);
        }
    }

    @GetMapping("getRelatedFunsBySceneId.do")
    @ApiOperation(value = "根据场景查询已关联的函数", notes = "函数库")
    @Validated
    public Response<List<FunctionRelatedSceneResult>> getRelatedFunsBySceneId(
            @ApiParam(value = "函数中/英文名") @RequestParam(value = "keyWord", required = false) String keyWord,
            @ApiParam(value = "场景id",required = true) @RequestParam(value = "sceneId") Integer sceneId){
        try {
            List<FunctionRelatedSceneResult> relatedFuns = functionService.getRelatedFunsBySceneId(keyWord, sceneId);
            return Response.ok(relatedFuns);
        } catch (BaseException e) {
            logger.error("getRelatedFunsBySceneId BaseException",e);
            return Response.error(e,null);
        } catch (Exception e) {
            logger.error("getRelatedFunsBySceneId Exception",e);
            return Response.error(e,null);
        }
    }

    @ResponseBody
    @PostMapping("updateFunctionTypeInfo.do")
    @ApiOperation(value = "更新函数类型信息", notes = "函数库")
    public Response<FunctionLibraryInfo> updateFunctionTypeInfo(
            @ApiParam(value = "函数类型请求对象") @RequestBody FunctionTypeReq reqParam){
        try {
            String userName = "system";
            functionService.updateFunctionTypeInfo(reqParam, userName);
            return Response.ok(null);
        } catch (BaseException e) {
            logger.error("updateFunctionTypeInfo BaseException",e);
            return Response.error(e,null);
        } catch (Exception e) {
            logger.error("updateFunctionTypeInfo Exception",e);
            return Response.error(e,null);
        }
    }

    @GetMapping("getFunctionLibraryInfoById.do")
    @ApiOperation(value = "根据id查询函数类型信息", notes = "函数库")
    public Response<FunctionLibraryInfo> getFunctionLibraryInfoById(
            @ApiParam(value = "函数类型id",required = true) @RequestParam(value = "fid") Integer fid){
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            FunctionLibraryInfo functionLibraryInfo = functionService.getFunctionLibraryInfoById(fid);
            return Response.ok(functionLibraryInfo);
        } catch (BaseException e) {
            logger.error("getFunctionLibraryInfoById BaseException",e);
            return Response.error(e,null);
        } catch (Exception e) {
            logger.error("getFunctionLibraryInfoById Exception",e);
            return Response.error(e,null);
        }
    }

//    @ResponseBody
//    @PostMapping("function/batchDelFunctions.do")
//    @ApiOperation(value = "根据函数id批量删除函数", notes = "函数库")
//    public Response<?> batchDelFunctions(
//            @ApiParam("函数id集合") @RequestParam(value = "functionIds",defaultValue = "1",required = false) String functionIds){
//        try {
//            String userName = getLoginUser();
//            Integer count = functionService.batchDelFunctions(functionIds, userName);
//            return Response.ok(null, "成功删除" + count + "条");
//        } catch (BaseException e) {
//            logger.error("batchDelFunctions BaseException", e);
//            return Response.error(e, null);
//        } catch (Exception e){
//            logger.error("batchDelFunctions Exception", e);
//            return Response.error(null);
//        }
//    }


}
